﻿/// SDMX Artefact Tiny Repository. A repository for SAE Editor.
///    Copyright (C) 2010  Philippe Prati 
///
///    This program is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 2 of the License, or
///    (at your option) any later version.
///
///    This program is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with this program.  If not, see <http://www.gnu.org/licenses/>.
///
///    Contact: ph.prati@bluewin.ch

using System;
using System.Xml;
using System.Collections.Generic;
using System.Web;
using System.Configuration;

/// <summary>
/// OwnerShipSupport manages group Artefact owners mechanism
/// </summary>
public class OwnerShipSupport
{
    private Dictionary<string, User> _dicoAdmin;
    #region Properties

    private bool _isUsed;
    public bool IsUsed
    {
        get { return _isUsed; }
    }

    private string _organisationSchemeFullId;
    public string OrganisationSchemeFullId
    {
         get { return _organisationSchemeFullId; }
    }

    private OwnerGroupDico _ownerGroupDico;
    public OwnerGroupDico DicoOwnerGroup
    {
        get { return _ownerGroupDico; }
    }

    #endregion

    #region C'tor

    public OwnerShipSupport()
	{
        _isUsed = false;
        _organisationSchemeFullId = string.Empty;
        _dicoAdmin = new Dictionary<string, User>();

        // Is used if Web config say it and Organization already present in Repository
        if (ConfigurationManager.AppSettings["GroupOrganisationScheme"] != null)
        {
            _organisationSchemeFullId = ConfigurationManager.AppSettings["GroupOrganisationScheme"].ToString();
            try
            {
                // Metadata Administrator(s)
                _dicoAdmin = new Dictionary<string, User>();
                if (ConfigurationManager.AppSettings["GroupFullAdministrators"] != null)
                {
                    string[] arr = ConfigurationManager.AppSettings["GroupFullAdministrators"].Split(';');
                    if (arr.Length > 0 && arr[0].Length > 0)
                    {
                        foreach (string uid in arr)
                        {
                            User u = new User(uid, GroupUserRole.Administrator);
                            if (_dicoAdmin.ContainsKey(u.Id) == false)
                                _dicoAdmin.Add(u.Id, u);
                        }

                        XmlDocument xDoc = new XmlDocument();
                        string path = Manager.Instance.RootPath + "OrganisationSchemes" + "\\" + _organisationSchemeFullId + ".xml";
                        
                        //Check if already exists
                        if (System.IO.File.Exists(path))
                        {
                            xDoc.Load(path);
                            UpdateGroupOwnerList(xDoc);
                        }
                    }
                }
            }
            catch 
            {
                _isUsed = false;
                _organisationSchemeFullId = string.Empty;
            }

        }
    }

    public void UpdateGroupOwnerList(XmlDocument xDoc)
    {
        ArtefactLocator locator = new ArtefactLocator(xDoc);
        _ownerGroupDico = locator.GenerateOwnerGroupInfo();
        _ownerGroupDico.DicoAdmin = _dicoAdmin;
        _isUsed = _ownerGroupDico.Count > 0;

    }


    #endregion

    public bool IsSystemAdmin(ref string user)
    {
        if (user == null)
            user = string.Empty;
        bool admin = false;
        if (_dicoAdmin != null && _dicoAdmin.ContainsKey(user.ToLower()))
            admin = true;

        return admin;
    }

}
